Page({
  data: {
    bartenders: [
      {
        name: '李白',
        title: '调酒宗师',
        avatar: '/images/bartender1.png',
        specialty: '青莲醉月',
        desc: '诗酒风流，才情横溢，擅长以诗入酒，作品清新脱俗。',
        motto: '举杯邀明月，对影成三人。',
        scores: {
          "颜值指数": 5,
          "幽默指数": 4,
          "口感指数": 5,
          "创意指数": 5,
          "古风指数": 5,
          "人气指数": 5
        }
      },
      {
        name: '杜甫',
        title: '品酒雅士',
        avatar: '/images/bartender2.png',
        specialty: '诗酒流年',
        desc: '沉稳内敛，幽默风趣，善于品鉴各类美酒，作品厚重悠长。',
        motto: '会当凌绝顶，一览众山小。',
        scores: {
          "颜值指数": 4,
          "幽默指数": 5,
          "口感指数": 4,
          "创意指数": 4,
          "古风指数": 5,
          "人气指数": 4
        }
      }
    ],
    perfumers: [
      {
        name: '王维',
        title: '香艺大师',
        avatar: '/images/perfumer1.png',
        specialty: '松风禅意',
        desc: '禅意悠远，气质高雅，擅长以自然为灵感调制香气。',
        motto: '行到水穷处，坐看云起时。',
        scores: {
          "颜值指数": 5,
          "幽默指数": 3,
          "味感指数": 5,
          "创意指数": 5,
          "古风指数": 5,
          "人气指数": 4
        }
      },
      {
        name: '薛涛',
        title: '香韵才女',
        avatar: '/images/perfumer2.png',
        specialty: '红豆香笺',
        desc: '才情出众，温婉细腻，擅长将诗意融入香气之中。',
        motto: '人生若只如初见。',
        scores: {
          "颜值指数": 5,
          "幽默指数": 4,
          "味感指数": 4,
          "创意指数": 5,
          "古风指数": 5,
          "人气指数": 5
        }
      }
    ],
    selected: null,
    selectedType: '',
    isDarkMode: false,
    locationText: '定位中...',
    bars: []
  },
  onLoad() {
    function getStars(score) {
      return '⭐️'.repeat(score) + '☆'.repeat(5 - score);
    }
    const addStars = (arr) => arr.map(person => {
      person.scoreArr = Object.keys(person.scores).map(key => ({
        label: key,
        value: person.scores[key],
        stars: getStars(person.scores[key])
      }));
      return person;
    });
    this.setData({
      bartenders: addStars(this.data.bartenders),
      perfumers: addStars(this.data.perfumers)
    });
  },
  selectPerson(e) {
    const { type, index } = e.currentTarget.dataset;
    let selected;
    if (type === 'bartender') {
      selected = this.data.bartenders[index];
    } else {
      selected = this.data.perfumers[index];
    }
    // 补丁：如果没有 scoreArr 字段，现生成
    if (!selected.scoreArr) {
      const getStars = score => '⭐️'.repeat(score) + '☆'.repeat(5 - score);
      selected.scoreArr = Object.keys(selected.scores).map(key => ({
        label: key,
        value: selected.scores[key],
        stars: getStars(selected.scores[key])
      }));
    }
    this.setData({ selected, selectedType: type });
  },
  closeDetail() {
    this.setData({ selected: null, selectedType: '' });
  },
  onShow() {
    const isDark = wx.getStorageSync('isDarkMode');
    this.setData({
      isDarkMode: typeof isDark === 'boolean' ? isDark : (getApp().globalData.isDarkMode || false)
    });
    this.getLocationAndBars();
  },
  refreshLocation() {
    this.getLocationAndBars();
  },
  getLocationAndBars() {
    wx.showLoading({ title: '定位中...' });
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const { latitude, longitude } = res;
        // 逆地理编码可用腾讯地图API或高德API，这里简单展示
        this.setData({ locationText: `经纬度：${latitude.toFixed(4)}, ${longitude.toFixed(4)}` });
        // 拉取附近合作酒吧
        wx.request({
          url: 'https://your-server.com/api/partners',
          method: 'GET',
          data: { lat: latitude, lng: longitude },
          success: (resp) => {
            if (resp.data && Array.isArray(resp.data.bars)) {
              this.setData({ bars: resp.data.bars });
            } else {
              this.setData({ bars: [] });
            }
          },
          fail: () => {
            this.setData({ bars: [] });
          },
          complete: () => {
            wx.hideLoading();
          }
        });
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({ title: '定位失败', icon: 'none' });
        this.setData({ locationText: '定位失败', bars: [] });
      }
    });
  },
  openMap(e) {
    const lat = Number(e.currentTarget.dataset.lat);
    const lng = Number(e.currentTarget.dataset.lng);
    wx.openLocation({ latitude: lat, longitude: lng, scale: 16 });
  },
  callBar(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({ phoneNumber: phone });
  }
}) 